package cn.itcast.zt.java8.algorithm;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 区间数据命中工具类
 * @author zhangtian105
 * @date 2023/4/4
 * @apiNote
 */
public class IntervalHitUtil {
    /*
        {
          "110800": {
            "countDown": 10,
            "unansweredToast": "https://img13.360buyimg.com/imagetools/jfs/t1/129506/31/35749/2142/6422821cF8930e975/8460f50b25c4de51.png",
            "answeredToast": "https://img10.360buyimg.com/imagetools/jfs/t1/184085/13/33472/3142/6419639fF790252dd/6ba5071fecb1b79a.png",
            "guidToast": "观看视频{}s后可以答题赢大奖哦~",
            "backgroundImg": "https://img10.360buyimg.com/imagetools/jfs/t1/169785/3/36210/285675/64227c29Fc94774dd/97dc0e3b11cfaea6.png",
            "startS": 0,
            "endS": 100,
            "whitePins": []
          },
          "110802": {
            "countDown": 10,
            "unansweredToast": "https://img13.360buyimg.com/imagetools/jfs/t1/129506/31/35749/2142/6422821cF8930e975/8460f50b25c4de51.png",
            "answeredToast": "https://img11.360buyimg.com/imagetools/jfs/t1/114616/40/32336/1335/642e6a71Fbe488c96/f92fdbba619c5fd6.png",
            "answerRightToast": "https://img14.360buyimg.com/imagetools/jfs/t1/109886/37/37726/1347/642e6a6cFd4d99815/36399141f39d0eb1.png",
            "guidToast": "观看视频{}s后可以答题赢大奖哦~",
            "reviewToast": "浏览{}s才可作答哦~",
            "backgroundImg": "https://img12.360buyimg.com/imagetools/jfs/t1/893/38/19963/52808/642e6a77Fe3645fcf/bf8fb49375060713.png",
            "startS": 0,
            "endS": 100,
            "whitePins": []
          }
        }
     */
    /**
     * 这里传参
     * @param treeMap  配置的版本对应数据 如上
     * @param paramsVersion 转换的版本 如：12.3.4 -> 120304
     * @return
     * @param <T>
     */
    public static <T> T clientVersionIntervalHit(TreeMap<String, T> treeMap, Integer paramsVersion) {
        Integer[] keyArr = new Integer[treeMap.size()] ;
        int m = 0 ;
        for (Map.Entry<String, T> entry : treeMap.entrySet()) {
            keyArr[m] = Integer.valueOf(entry.getKey()) ;
            m++ ;
        }

        String lastKey = treeMap.lastKey() ;
        if (ArrayUtil.isNotEmpty(keyArr)) {
            for (int i = 0; i < keyArr.length; i++) {
                Integer currentKey = keyArr[i] ;
                if (currentKey.equals(Integer.valueOf(lastKey))) { // 是最后一个key
                    if (paramsVersion >= currentKey) {
                        return treeMap.get(String.valueOf(currentKey)) ;
                    }
                }else {
                    Integer nextKey = keyArr[i + 1] ;
                    if (paramsVersion >= currentKey && paramsVersion < nextKey) {
                        return treeMap.get(String.valueOf(currentKey)) ;
                    }
                }
            }
        }

        return null ;
    }

    public static Integer exchangeClientVersion(String clientVersion) {
        if (StrUtil.isBlank(clientVersion)) {
            return 0 ;
        }

        List<String> versionList = StrUtil.split(clientVersion, ".");
        StringBuilder sb = new StringBuilder() ;
        for (int i = 0; i < versionList.size(); i++) {
            sb.append(String.format("%02d", Integer.valueOf(versionList.get(i)))) ;
        }

        return Integer.valueOf(sb.toString()) ;
    }

    public static void main(String[] args) {
        TreeMap<String, String> treeMap = new TreeMap<String, String>() ;
        treeMap.put("110800", "a") ;
        treeMap.put("110802", "b") ;
        treeMap.put("110804", "c") ;

        // 这里是判断版本在 传参版本以及以上下发的配置信息（配置项中必须要配置传参版本以及之前的配置数据）
        // 这里11.6.0版本配置项返回null 因为没有配置最低版本
        // 这里11.8.6版本的话返回即是离其最近的版本配置项
        System.out.println(IntervalHitUtil.clientVersionIntervalHit(treeMap, 110600));
    }
}
